<?php

//$tag is the content tag, need to split it to paragraphes. the title may be in the first 3 paragraphes
//at the same time, get the pre 3 tags , which may has the title infor either
//$leaf_tag_hz_num is a important array with the hz info. those tag with no-hz child is regarded as leaf tag

//to get the pre tags, use the $leaf_tag_hz_num array
define('REMOVE_ONE_HZ_PARAGRAPH',1);
function removeNonChinese($str)
{
	$p=preg_replace("/[A-Za-z_<>\/&;:]/","",$str);
	$p=preg_replace("/(^ *)|( *)$/","",$p);
	$p=preg_replace("/^　*|　*$/u","",$p);
	$p=preg_replace("/ /u","　",$p);
	$p=preg_replace("/　+/u","　",$p);
	return $p;
}
function removeNewLine($str)
{
	$p=preg_replace("/\x0a/","",$str);
	return $p;
}
//some single node is processed here as well, where the <br> is not regarded as child node
function split_pre($tag)
{
	global $chapter_paragraphes;
	global $title_candidates;
	echo "handle pre tag\n";
	//replace the 0a/0d/0a0d * with BR
	$cont=toutf8($tag->innertext);
	//$cont=preg_replace("/[ 　]*/","",$cont);
	$cont=preg_replace("/br/","BR",$cont);
	$cont=preg_replace("/(\x0a|\x0d|\x0d\x0a)[ 　]+/","<BR>",$cont);
	//remove the blank
	$ps=explode("<BR>",$cont);

	$cnt = 0 ; 	
	$stop=0;
	foreach( $ps as $p ) {
		$p=removeNonChinese($p);
		$p=removeNewLine($p);
		$hz=count_hz_num($p) ;
		if($hz > 1){
			array_push($chapter_paragraphes,$p);
		}
		if($cnt < 3 && $hz < TITLE_MAX_LEN && $stop!=1){
			array_push($title_candidates,$p);
		}else{
			$stop=1;
		}
		$cnt ++; 
	}
}
function split_nonpre($tag)
{
	global $title_candidates;
	global $chapter_paragraphes;
	global $leaf_tag_hz_num; 
	global $html;
	global $path; //root path
	//for each child , if it hz num > 0 and < MAXLEN, add to candidate
	echo "handle nonpre tag\n";
	//adding title candidate
//	foreach($tag->childNodes() as $ch){
//		$cnt=count_hz_num($ch->innertext) ;
//		if($cnt < TITLE_MAX_LEN){
//			if($cnt>0){
//				echo "get a title candidate : " . $ch->tag ;
//				array_push($title_candidates,$ch->innertext); 	
//			}
//		}else{
//			break;
//		}
//	}a
	$path_array=array();
	$cnt=0;
	$stop=0;
	foreach($leaf_tag_hz_num as $t=>$v){
		if(strstr($t,$path)){
			echo "get a str ". $t . " contain path:" . $path . "\n";
			//if(!preg_match("/ a /",$t) ){ continue ; } 
			$con=get_content_by_tag($t);
			$con=removeNonChinese($con);
			$con=removeNewLine($con);
			if( $v < TITLE_MAX_LEN  && $cnt < 5 && $stop!=1) {
				array_push($title_candidates,$con);	
				$cnt ++ ;
			}else{
				$stop = 1 ;
			}
			if(REMOVE_ONE_HZ_PARAGRAPH ==1 && $v==1 ){
			}else{
				array_push($chapter_paragraphes,$con);
			}
		}
	}

	foreach($path_array as $p){
	}
	
}
function add_title_candidate_outof_content($tag)
{
	global $title_candidates;
	global $leaf_tag_hz_num; 
	global $html;
	global $path;

	$candidate=array();
	foreach($leaf_tag_hz_num as $t=>$n){
		if(!preg_match("/ a /",$t) && !preg_match("/ style /",$t)){
			echo "find a !<a> tag: " . $t . "\n" ;
			if(!strstr($t,$path)){   //it is not a child of the $tag
				echo  $t . " is a title candidate\n" ;
				array_push($candidate,$t);
			}else{
				break;
			}
		}else{
			echo "find a <a> tag: " . $t . "\n" ;
		}
	}
	echo "all the candidate:\n";
	print_r($candidate);
	while(count($candidate) > 3){
		array_shift($candidate);
	}
	echo "all the candidate after remove:\n";
	print_r($candidate);
	
	$title_candidates=$candidate;
}
add_title_candidate_outof_content($tag);
$title_candidates_t=array();
array_unshift($title_candidates_t,get_title($html));
foreach($title_candidates as $tt){
	echo "tag $tt for title\n";
	$con=get_content_by_tag($tt) ; 
	$con=removeNonChinese($con);
	$con=removeNewLine($con);
	echo "con=" . $con . "\n";
	array_push($title_candidates_t,$con);
}
$title_candidates = $title_candidates_t ; 


if(isLeaf($tag)) { //$tag->tag == "pre"){
	split_pre($tag);
}else{
	echo "content is not in leaf\n" ;
	//echo toutf8($tag->plaintext) ; 
	//tranverse_html($tag,"print_innertext");	
	//return ; 
	split_nonpre($tag);
}
echo "title candidates\n";
print_r($title_candidates);
//print_r($chapter_paragraphes);

require("dic.php");

?>
